//
//  AppInlines.h
//  SmartVideo
//
//  Created by nsm on 2019/3/7.
//  Copyright © 2019 nani. All rights reserved.
//

#ifndef AppInlines_h
#define AppInlines_h

static inline UIColor *HexColor(NSString *colorStr) {
    UIColor *color = [UIColor colorWithHexString:colorStr];
    if (!color) color = UIColor.lightGrayColor;
    return color;
}

static inline CGPoint CGSizeCenter(CGSize size) {
    return CGPointMake(size.width/2, size.height/2);
}

static inline UIColor *RGBAColor(CGFloat r, CGFloat g, CGFloat b, CGFloat a) {
    return [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a/1.0f];
}

static inline UIColor *RGBColor(CGFloat r, CGFloat g, CGFloat b) {
    return RGBAColor(r, g, b, 1);
}

#pragma mark - SafeObject
static inline BOOL SafeObject(id obj) {
    return obj && ![obj isEqual:NSNull.null];
}

static inline NSString *SafeString(id obj) {
    BOOL isValid = SafeObject(obj) && [obj isKindOfClass:NSString.class];
    return isValid? obj: @"";
}

static inline NSArray *SafeArray(id obj) {
    BOOL isValid = SafeObject(obj) && [obj isKindOfClass:NSArray.class];
    return isValid? (NSArray *)obj: @[];
}

static inline NSDictionary *SafeDictionary(id obj) {
    BOOL isValid = SafeObject(obj) && [obj isKindOfClass:NSDictionary.class];
    return isValid? (NSDictionary *)obj: @{};
}

static inline NSMutableAttributedString *AttributedStringFL(NSString *str,
                                                            UIFont *font,
                                                            UIColor *color) {
    NSMutableAttributedString *attrM = [[NSMutableAttributedString alloc] initWithString:SafeString(str)];
    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
    paragraph.lineSpacing = 0;
    paragraph.alignment = NSTextAlignmentLeft;
    [attrM addAttributes:@{NSParagraphStyleAttributeName: paragraph,
                           NSFontAttributeName: font,
                           NSForegroundColorAttributeName: color}
                   range:NSMakeRange(0, str.length)] ;
    return attrM;
}

static inline NSMutableAttributedString *AttributedStringFR(NSString *str,
                                                            UIFont *font,
                                                            UIColor *color) {
    NSMutableAttributedString *attrM = [[NSMutableAttributedString alloc] initWithString:SafeString(str)];
    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
    paragraph.lineSpacing = 0;
    paragraph.alignment = NSTextAlignmentRight;
    [attrM addAttributes:@{NSParagraphStyleAttributeName: paragraph,
                           NSFontAttributeName: font,
                           NSForegroundColorAttributeName: color}
                   range:NSMakeRange(0, str.length)] ;
    return attrM;
}


static inline NSMutableAttributedString *AttributedStringFC(NSString *str,
                                                            UIFont *font,
                                                            UIColor *color) {
    NSMutableAttributedString *attrM = [[NSMutableAttributedString alloc] initWithString:SafeString(str)];
    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
    paragraph.lineSpacing = 0;
    paragraph.alignment = NSTextAlignmentCenter;
    [attrM addAttributes:@{NSParagraphStyleAttributeName: paragraph,
                           NSFontAttributeName: font,
                           NSForegroundColorAttributeName: color}
                   range:NSMakeRange(0, str.length)] ;
    return attrM;
}

static inline NSMutableAttributedString *AttributedString(NSString *preStr,
                                                          UIFont *preFont,
                                                          UIColor *preColor,
                                                          NSString *sufStr,
                                                          UIFont *sufFont,
                                                          UIColor *sufColor,
                                                          NSTextAlignment alignment,
                                                          CGFloat lineSpacing) {
    NSString *prefix = SafeString(preStr);
    NSString *suffix = SafeString(sufStr);
    NSString *str = [NSString stringWithFormat:@"%@%@", prefix, suffix];
    NSMutableAttributedString *attrM = [[NSMutableAttributedString alloc] initWithString:str];
    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
    paragraph.lineSpacing = lineSpacing;
    paragraph.alignment = alignment;
    [attrM addAttribute:NSParagraphStyleAttributeName
                  value:paragraph
                  range:NSMakeRange(0, str.length)];
    [attrM addAttributes:@{NSFontAttributeName: preFont,
                           NSForegroundColorAttributeName: preColor}
                   range:NSMakeRange(0, prefix.length)];
    [attrM addAttributes:@{NSFontAttributeName: sufFont,
                           NSForegroundColorAttributeName: sufColor}
                   range:NSMakeRange(prefix.length, suffix.length)];
    return attrM;
}


#pragma mark - UIView
static inline void ViewBorder(UIView *view,
                              CGFloat borderWidth,
                              UIColor *borderColor) {
    view.layer.borderWidth = borderWidth;
    view.layer.borderColor = borderColor.CGColor;
}

static inline void ViewCorner(UIView *view,
                              CGFloat cornerRadius) {
    view.layer.masksToBounds = YES;
    view.layer.cornerRadius = cornerRadius;
}


#pragma mark - 颜色
#define COLOR_0                 ([UIColor blackColor])
#define COLOR_1                 ([UIColor darkGrayColor])
#define COLOR_2                 ([UIColor grayColor])
#define COLOR_3                 ([UIColor lightGrayColor])
#define COLOR_4                 ([UIColor whiteColor])
#define COLOR_5                 ([UIColor clearColor])
//常用主文本
#define BLACkCOLOR              ([UIColor blackColor])
#define COLOR1515                HexColor(@"0x151515")
#define COLOR333                 HexColor(@"0x333333")
//常用黑色文本
#define COLOR222                 HexColor(@"#222222")
//常用副文本
#define COLOR666                 HexColor(@"0x666666")
//常用灰色
#define COLOR999                 HexColor(@"0x999999")
// 主题色
#define THEME_COLOR             HexColor(@"0x37B0A6")
#define THEME_SEDCOLOR          HexColor(@"0xFF6726")
//背景色
#define BACKGROUNDCOLOR         HexColor(@"0xF1F4F4")

#define FormatString(fmt, ...)  [NSString stringWithFormat:fmt, ##__VA_ARGS__]
#define WeakSelf __weak typeof(self) weakSelf = self;
#define WeakObj(o) __weak typeof(o) o##Weak = o;
#define KeyWindow               [UIApplication sharedApplication].windows.firstObject

#define kAutoWidth              (MyLayoutSize.wrap)
#define kAutoHeight             (MyLayoutSize.wrap)
#define kAutoSize               CGSizeMake(MyLayoutSize.wrap, MyLayoutSize.wrap)
#define randomColor  [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0) green:((float)arc4random_uniform(256) / 255.0) blue:((float)arc4random_uniform(256) / 255.0) alpha:1.0];

#define _notification  [NSNotificationCenter defaultCenter]
#define _userdefaults  [NSUserDefaults standardUserDefaults]

#define BuildNumber         [[[NSBundle mainBundle]infoDictionary] objectForKey:@"CFBundleVersion"];
#define VersionNumber       [[[NSBundle mainBundle]infoDictionary] objectForKey:@"CFBundleShortVersionString"];
// 手环项目的宏
#define loginSuccess        @"loginSuccess"
#define VerificationCodeNumber   60

#endif /* AppInlines_h */
